package org.example.blogserver.blog.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.example.blogcommon.base.utils.R;

import org.example.blogpojo.Dto.ArticleDto;
import org.example.blogpojo.blog.domain.Article;
import org.example.blogpojo.blog.query.ArticleQuery;
import org.example.blogpojo.blog.vo.GdVo;
import org.example.blogserver.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping ("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping
    @Operation (summary = "查所有", description = "查所有")
    public R findAll() {
        List<Article> allArticle = articleService.getAllArticle();
        return R.ok(allArticle);
    }

    @GetMapping ("/{id}")
    @Operation (summary = "id查", description = "id查")
    public R findAll(@PathVariable Long id) {
        Article articleById = articleService.getArticleById(id);
        return R.ok(articleById);
    }

    @PostMapping
    @Operation (summary = "分页条件查询", description = "分页条件查询")
    public R queryPage(@RequestBody ArticleQuery articleQuery) {
        PageInfo<Article> articlePageInfo = articleService.queryPage(articleQuery);
        System.out.println(articlePageInfo);
        return R.ok(articlePageInfo);
    }

    @PutMapping
    @Operation (summary = "新增修改", description = "新增修改")
    public R insertandupdate(@RequestBody Article article) {
        try {
            Long id = article.getId();
            if (id == null) {
                articleService.insertArticle(article);
            } else {
                articleService.updateArticle(article);
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail();
    }

    @DeleteMapping ("/{id}")
    @Operation (summary = "通过id删除", description = "通过id删除")
    public R deleteById(@PathVariable Long id) {
        articleService.deleteArticle(id);
        return R.ok();
    }

    @PatchMapping
    @Operation (summary = "批量删除", description = "通过id批量删除")
    public R deleteByIds(@RequestBody Long[] ids) {
        articleService.deleteArticleByIds(ids);
        return R.ok();
    }

    @GetMapping ("/gd")
    @Operation (summary = "归档需要日期和数量", description = "归档需要日期和数量")
    public List<GdVo> getGd() {
        List<GdVo> gd = articleService.getGd();
        return gd;
    }

    @GetMapping ("/ra/{id}")
    public List<Article> getRa(@PathVariable Long id) {
        List<Article> list = articleService.getRa(id);//假设法
        return list;//alt enter
    }

    @GetMapping ("/getBlogGdList/{time}")
    public List<ArticleDto> getBlogGdList(@PathVariable String time) {
        List<ArticleDto> blogGdList = articleService.getBlogGdList(time);//假设法
        return blogGdList;
    }
}
